﻿using Autodesk.AutoCAD.ApplicationServices;
using CADIfox_MVVM.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;

namespace CADIfox_MVVM.Command
{
    public class AddButtonCommand
    {
        [CommandMethod("AddButton")]
        public void AddButton()
        {
            var view = new ShowExcelView();
            view.ShowDialog();
        }

        [CommandMethod("Learn")]
        public void Learn()
        {
            var view = new LearnToolkitView();
            Application.ShowModalWindow(view);
            //view.ShowDialog();
        }

        [CommandMethod("CreateBox")]
        public void AddThree()
        {
            var view = new ThreeView();
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var db = HostApplicationServices.WorkingDatabase;
            //Application.ShowModalWindow(view);
            using var loc = doc.LockDocument();
            view.Show();
            loc.Dispose();
        }

        [CommandMethod("WW", CommandFlags.UsePickSet)]
        public void MakeLayerHide()
        {
            using var tr = new DBTrans();

            var impliedSelect = tr.Editor.SelectImplied();
            if (impliedSelect.Status == PromptStatus.OK)
            {
                var seletedObjects = impliedSelect.Value.GetObjectIds();
                var layerids = new List<ObjectId>();
                foreach (var seletedObject in seletedObjects)
                {
                    var entity = tr.GetObject<Entity>(seletedObject);
                    layerids.Add(entity.LayerId);
                }
                var layers = tr.LayerTable.Except(layerids).ToList();
                foreach (var layer in layers)
                {
                    HideLyaer(tr, layer);
                }
            }
        }

        [CommandMethod("EE", CommandFlags.UsePickSet)]
        public void MakeLayerHided()
        {
            using var tr = new DBTrans();

            var layerTbale = tr.LayerTable;
            layerTbale.ForEach(t =>
            {
                var layer = tr.GetObject<LayerTableRecord>(t, OpenMode.ForWrite);
                layer.IsOff = false;
            });
        }

        [CommandMethod(nameof(Apart), CommandFlags.UsePickSet)]
        public static void Apart()
        {
            using var tr = new DBTrans();

            var impliedSelect = tr.Editor.SelectImplied();
            if (impliedSelect.Status == PromptStatus.OK)
            {
                var seletedObjects = impliedSelect.Value.GetObjectIds();
                var layerids = new List<ObjectId>();
                foreach (var seletedObject in seletedObjects)
                {
                    var entity = tr.GetObject<Entity>(seletedObject);
                    layerids.Add(entity.LayerId);
                }
                var layers = tr.LayerTable.Except(layerids).ToList();
                foreach (var layer in layers)
                {
                    HideLyaer(tr, layer);
                }
            }
        }
        [CommandMethod("dd", CommandFlags.UsePickSet)]
        public static void ShowAll()
        {
            using var tr = new DBTrans();

            var layerTbale = tr.LayerTable;
            layerTbale.ForEach(t =>
            {
                var layer = tr.GetObject<LayerTableRecord>(t, OpenMode.ForWrite);
                layer.IsOff = false;
            });
        }

        public static void ApartLayer(DBTrans tr, ObjectId objectId)
        {
            var layers = tr.LayerTable.Where(x => x != objectId);
            foreach (var layer in layers)
            {
                HideLyaer(tr, layer);
            }
        }

        public static void HideLyaer(DBTrans tr, ObjectId objectId)
        {
            var layer = tr.GetObject<LayerTableRecord>(objectId, OpenMode.ForWrite);
            layer.IsOff = true;
        }

    }
}
